﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;

namespace Haha.Mvc
{
    public class MvcDown
    {
        public static MvcDown New
        {
            get
            {
                return new MvcDown();
            }
        }
        private byte[] data;
        public byte[] Data
        {
            get
            {
                return data;
            }
        }
        public MvcDown SetData(byte[] data)
        {
            this.data = data;
            return this;
        }
        private string fileName;
        public string FileName
        {
            get
            {
                return fileName;
            }
        }
        public MvcDown SetFileName(string fileName)
        {
            this.fileName = fileName;
            return this;
        }
    }

    public class MvcImage
    {
        public static MvcImage New
        {
            get
            {
                return new MvcImage();
            }
        }
        private byte[] data;
        public byte[] Data
        {
            get
            {
                return data;
            }
        }
        public MvcImage SetData(byte[] data)
        {
            this.data = data;
            return this;
        }
       
    }
    public class MvcContext
    {
        private HttpContext ctx;
        public HttpContext Ctx
        {
            get
            {
                return ctx;
            }
        }
        private HttpRequest req;
        public HttpRequest Request
        {
            get
            {
                return req;
            }
        }
        private HttpResponse resp;
        public HttpResponse Response
        {
            get
            {
                return resp;
            }
        }
        private HttpSessionState session;
        public HttpSessionState Session
        {
            get
            {
                return session;
            }
        }
        private HttpApplicationState application;
        public HttpApplicationState Application
        {
            get
            {
                return application;
            }
        }
        public static MvcContext MvcCtx = new MvcContext();
        internal static void Init(HttpContext ctx)
        {
            MvcCtx.ctx = ctx;
            MvcCtx.req = ctx.Request;
            MvcCtx.resp = ctx.Response;
            MvcCtx.session = ctx.Session;
            MvcCtx.application = ctx.Application;
            
        }

    }

    public class MvcData
    {
        private string data;
        public static MvcData Init(string data)
        {
            var me = new MvcData();
            me.data = data;
            return me;

        }
        public int? ToInt2(int? def=null)
        {
            if (data == null) return def;
            try { return int.Parse(data); }
            catch { return def; };
        }
        public int ToInt(int def=0)
        {
            if (data == null) return def;
            try { return int.Parse(data); }
            catch { return def; };
        }

        public double? ToDouble2(double? def = null)
        {
            if (data == null) return def;
            try { return double.Parse(data); }
            catch { return def; };
        }
        public double ToDouble(double def = 0)
        {
            if (data == null) return def;
            try { return double.Parse(data); }
            catch { return def; };
        }

        public decimal? ToDecimal2(decimal? def = null)
        {
            if (data == null) return def;
            try { return decimal.Parse(data); }
            catch { return def; };
        }
        public decimal ToDecimal(decimal def = 0)
        {
            if (data == null) return def;
            try { return decimal.Parse(data); }
            catch { return def; };
        }
        public string ToStr()
        {
            return data;
        }
        public DateTime ToDateTime(DateTime def)
        {
            if (data == null) return def;
            try { return DateTime.Parse(data); }
            catch { return def; };
        }
        public DateTime? ToDateTime2(DateTime? def=null)
        {
            if (data == null) return def;
            try { return DateTime.Parse(data); }
            catch { return def; };
        }
        public DateTime ToDateTimeDefaultNow()
        {
            if (data == null) return DateTime.Now;
            try { return DateTime.Parse(data); }
            catch { return DateTime.Now; };
        }

    }
    public class MvcFormData
    {
        private NameValueCollection datas;
        public static MvcFormData New
        {
            get
            {
                return new MvcFormData().Init();
            }
        }
        private MvcFormData Init()
        {
            datas = MvcContext.MvcCtx.Request.Params;
            return this;
        }

        public MvcData this[String name]
        {
            get
            {
                return  MvcData.Init(datas[name]);
            }
        }
    }
}
